﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


namespace TotalSport
{
    public partial class LoginUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (!IsPostBack)
            {
                BAL.Customers.PopulateLocation(ref ddlLocation);
                clear();
                int count = ddlLocation.Items.Count;
                ListItem list = new ListItem("Admin", "Admin");
                ddlLocation.Items.Insert(count, list);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {           
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string location = ddlLocation.SelectedValue;
            string strpas = BAL.Encryption.Encript(password, "&%#@?,:*");

            int userid = BAL.Customers.verifyuser(username, strpas, location);
            Session.Add("Location", location);




            if (userid == -2)
            {
                SMessage.Style.Add("Display", "block");
                SMessage.InnerHtml = "Sorry,Username does not exist";
            }
            else if (userid == -1)
            {
                SMessage.Style.Add("Display", "block");
                SMessage.InnerHtml = "Sorry,Username not verified ";
            }
            else if (userid == -4)
            {
                SMessage.Style.Add("Display", "block");
                SMessage.InnerHtml = "Sorry,Incorrect Login ";

            }
            else if (userid == 0)
            {
                SMessage.Style.Add("Display", "block");
                SMessage.InnerHtml = "Sorry Incorrect Login";
            }
            else
            {
                Session.Add("UserId", userid);
                Response.Redirect("AddProducts.aspx");
            }

            clear();


        }

        public void clear()
        {
            txtPassword.Text = "";
            txtUsername.Text = "";
        }


    }
}